package cn.witsky.cdrService.domain.base;

import cn.witsky.cdrService.constant.ValType;
import cn.witsky.cdrService.util.StringUtil;

public class Value {
	public String type;
	public String valStr;
	public String prefix;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getValStr() {
		return valStr;
	}
	public void setValStr(String valStr) {
		this.valStr = valStr;
	}
	public String getPrefix() {
		return prefix;
	}
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}	
	
	public byte[] toByte() {
		byte[] bytes=null;
		if(StringUtil.isEmpty(valStr)) {
			return bytes;
		}
		String newStr="";
		char[] charArr = valStr.toCharArray();
		char[] newArr=new char[2];
		if(type.equals(ValType.TIME)) {
			bytes=new byte[9];
			for(int i=0;i<6;i++) {
				newArr[0]=charArr[i*2];
				newArr[1]=charArr[i*2+1];
				newStr=String.valueOf(newArr);
				bytes[i]=(byte) StringUtil.decodeHEX(newStr);
			}
			bytes[6]=(byte)0x2b;
			bytes[7]=(byte)0x08;
			bytes[8]=(byte)0x00;
		}else if(type.equals(ValType.TBCD)) {			
			int len=valStr.length();
			int count=len/2;
			int remainder=len-count*2;
			boolean hasPrefix=!StringUtil.isEmpty(this.getPrefix());
			if(hasPrefix) {
				count++;
			}
			if(remainder==0) {	
				bytes=new byte[count];						
			}else {
				bytes=new byte[count+1];
			}
			if(hasPrefix) {
				bytes[0]=(byte) StringUtil.decodeHEX(this.getPrefix());
				for(int i=0;i<count-1;i++) {
					newArr[1]=charArr[i*2];
					newArr[0]=charArr[i*2+1];
					newStr=String.valueOf(newArr);
					if(hasPrefix) {
						bytes[i+1]=(byte) StringUtil.decodeHEX(newStr);
					}else {
						bytes[i]=(byte) StringUtil.decodeHEX(newStr);
					}
				}
			}else {
				for(int i=0;i<count;i++) {
					newArr[1]=charArr[i*2];
					newArr[0]=charArr[i*2+1];
					newStr=String.valueOf(newArr);
					if(hasPrefix) {
						bytes[i+1]=(byte) StringUtil.decodeHEX(newStr);
					}else {
						bytes[i]=(byte) StringUtil.decodeHEX(newStr);
					}
				}
			}	

			if(remainder>0) {
				newArr[0]='f';
				newArr[1]=charArr[len-1];
				newStr=String.valueOf(newArr);
				bytes[count]=(byte) StringUtil.decodeHEX(newStr);
			}
		}else if(type.equals(ValType.ASCII)){
			int len=valStr.length();
			bytes=new byte[len];
			for(int i=0;i<len;i++) {
				bytes[i]=(byte) charArr[i];
			}
		}else if(type.equals(ValType.DecToHex)){
			String hexStr= Integer.toHexString(Integer.parseInt(valStr));			
			int len=hexStr.length();
			int count=len/2;
			if(len>count*2) {
				count=count+1;
				hexStr="0"+hexStr;
			}
			charArr=hexStr.toCharArray();
			bytes=new byte[count];	
			for(int i=0;i<count;i++) {
				newArr[0]=charArr[i*2];
				newArr[1]=charArr[i*2+1];
				newStr=String.valueOf(newArr);
				bytes[i]=(byte) StringUtil.decodeHEX(newStr);
			}
		}else {
			int len=valStr.length();
			int count=len/2;
			bytes=new byte[count];	
			for(int i=0;i<count;i++) {
				newArr[0]=charArr[i*2];
				newArr[1]=charArr[i*2+1];
				newStr=String.valueOf(newArr);
				bytes[i]=(byte) StringUtil.decodeHEX(newStr);
			}
		}
		return bytes;
	}
	public void clear() {
		this.prefix=null;
		this.type=null;
		this.valStr=null;
	}
}
